package com.eorchis.module.auditing.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.entity.IBaseEntity;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.service.impl.AbstractBaseService;

import com.eorchis.module.TrainingConstants;
import com.eorchis.module.auditing.dao.IAuditingDao;
import com.eorchis.module.auditing.domain.Auditing;
import com.eorchis.module.auditing.domain.AuditingBean;
import com.eorchis.module.auditing.service.IAuditingService;
import com.eorchis.module.auditing.ui.commond.AuditingQueryCommond;
import com.eorchis.module.auditing.ui.commond.AuditingValidCommond;
import com.eorchis.module.classapplydep.dao.IClassApplyDepDao;
import com.eorchis.module.classapplydep.ui.commond.ClassApplyDepQueryCommond;
import com.eorchis.module.classuser.dao.IClassUserDao;
import com.eorchis.module.classuser.domain.ClassUser;
import com.eorchis.module.trainingclass.dao.ITrainingClassDao;
import com.eorchis.module.trainingclass.domain.TrainingClass;
import com.eorchis.module.trainingclass.domain.TrainingClassQueryBean;
import com.eorchis.module.trainingclass.ui.commond.TrainingClassQueryCommond;
import com.eorchis.module.user.domain.User;

/**
 * Title: AuditingServiceImpl<br>
 * Description: 班级审核人员信息管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Max
 * @createDate 2013-12-31
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.auditing.service.impl.AuditingServiceImpl")
public class AuditingServiceImpl 
			extends AbstractBaseService 
		implements IAuditingService {
		
	@Autowired
	@Qualifier("com.eorchis.module.auditing.dao.impl.AuditingDaoImpl")
	private IAuditingDao auditingDao;
	
	@Autowired
	@Qualifier("com.eorchis.module.trainingclass.dao.impl.TrainingClassDaoImpl")
	private ITrainingClassDao trainingClassDao;
	
	@Autowired
	@Resource(name = "main.java.com.eorchis.module.classapplydep.dao.impl.ClassApplyDepDaoImpl")
	private IClassApplyDepDao classApplyDepDao;
	
	@Resource(name = "com.eorchis.module.classuser.dao.impl.ClassUserDaoImpl")
	private IClassUserDao classUserDao;
	
	public IDaoSupport getDaoSupport() {
		return auditingDao;
	}
	@SuppressWarnings("unchecked")
	public AuditingValidCommond toCommond(IBaseEntity entity) {
		return new AuditingValidCommond((Auditing)entity);
	}
	/**
	 * 批量审核人员
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Ningss
	 * @createDate 2013-12-30
	 */
	@Override
	public void batchAuditing(AuditingQueryCommond queryCommond)throws Exception {
		String[] s=queryCommond.getSearchClassUserIds();
		for(int i=0;i<queryCommond.getSearchClassUserIds().length;i++){
			Auditing auditing=new Auditing();
			auditing.setAuditingState(queryCommond.getAuditingState());
			auditing.setAuditingOpinion(queryCommond.getAuditingOpinion());
			auditing.setAuditingType(queryCommond.getAuditingType());
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String strDate = queryCommond.getAuditingStateStr();
			Date date=sdf.parse(strDate); 
			auditing.setAuditingDate(date);
			User user=new User();
			user.setUserId(queryCommond.getAuditingUserId());
			auditing.setAuditingUser(user);
			ClassUser classUser=new ClassUser();
			classUser.setClassUserId(s[i]);
			auditing.setClassUser(classUser);
			auditingDao.save(auditing);
		}
		//当为审核补报名人员时需要判断审核通过人数是否超过班级人数，如果超过需要重新设置班级人数上限
		if(1==queryCommond.getAuditingState()&&TrainingConstants.SHRY.equals(queryCommond.getAuditingType())){
			int reportedNum=0;//已报人数
			int classUserNum=0;//班级人数上限(或班级限报人数)
			//获取已报人数
			TrainingClassQueryCommond trainingClassQueryCommond=new TrainingClassQueryCommond();
			trainingClassQueryCommond.setSearchClassId(queryCommond.getSearchClassID());
			//List<TrainingClassQueryBean> classList=trainingClassDao.findTrainingClassList(trainingClassQueryCommond);
			List<TrainingClassQueryBean> classList=new ArrayList<TrainingClassQueryBean>();
			reportedNum=classList.get(0).getReportedNum().intValue();
			classUserNum=classList.get(0).getClassUserNum();
			int num=reportedNum-classUserNum;
			if(num>0){//已超过班级人数
				classUserDao.updateClassUserNum(queryCommond.getSearchClassID(),reportedNum);
			}
			//当为审核区级项目(指定报名机构)补报名人员时需要判断审核通过人数是否超过班级限制人数，如果超过需要重新设置班级限制人数
			if(TrainingConstants.QJXMBM.equals(queryCommond.getReplenishApplyUserType())&&"zdbmjg".equals(queryCommond.getSetApplyOrgan())){
				//选中人员所在的单位并去重
				String[] deptIds=queryCommond.getDeptIds();
				List<String> list = new ArrayList<String>();  
			    for (int i=0; i<deptIds.length; i++) {  
					if(!list.contains(deptIds[i])) {  
					list.add(deptIds[i]);  
					}  
				}  
				for(String deptId:list){
					//获取已报人数
					TrainingClassQueryCommond tcqc=new TrainingClassQueryCommond();
					tcqc.setSearchClassId(queryCommond.getSearchClassID());
					tcqc.setSearchDepId(deptId);
					tcqc.setSearchClassCategory(TrainingClass.Q_CLASS_TYPE);//只查区级班
					tcqc.setSearchActiveState(TrainingConstants.IS_ACTIVE_Y);//启用状态
					tcqc.setSearchDataCode(TrainingConstants.BZDBMJG);//不指定报名机构
					//List<TrainingClassQueryBean>  trainingClassQueryBeans=trainingClassDao.findOrganizationTrainingClassList(tcqc);
					List<TrainingClassQueryBean> trainingClassQueryBeans=new ArrayList<TrainingClassQueryBean>();
					TrainingClassQueryBean trainingClassQueryBean=trainingClassQueryBeans.get(0);
					reportedNum=trainingClassQueryBean.getReportedNum().intValue();
					classUserNum=trainingClassQueryBean.getRestrictPersonNum().intValue();
					int number=reportedNum-classUserNum;
					if(number>0){//已超过班级限报人数
						ClassApplyDepQueryCommond classApplyDepQueryCommond=new ClassApplyDepQueryCommond();
						classApplyDepQueryCommond.setClassId(queryCommond.getSearchClassID());
						classApplyDepQueryCommond.setDepId(deptId);
						classApplyDepDao.updateRestrictPersonNum(classApplyDepQueryCommond,trainingClassQueryBean.getReportedNum());
					}
				}
			}
		}
	}
	/**
	 * 查询审核信息
	 * @param queryCommondst
	 * @return
	 * @throws Exception
	 * @author HuangTF
	 * @createDate 2013-12-31
	 */
	public List<AuditingBean> findAuditingInfo(AuditingQueryCommond queryCommond)
	{
		return auditingDao.findAuditingInfo(queryCommond);
	}
}
